package two_pointer;

/**
 * Created with IntelliJ IDEA.
 * Description: 27.移除元素
 * User: Li_yizYa
 * Date: 2025—04—17
 * Time: 21:46
 */
public class Solution1 {
    public static void main(String[] args) {
        int[] nums = {0,1,2,2,3,0,4,2};
        int n = removeElement(nums, 2);
        System.out.println(n);
        for (int i = 0; i < n; i++) {
            System.out.print(nums[i] + " ");
        }
        System.out.println();
        for (int i = 0; i < nums.length; i++) {
            System.out.print(nums[i] + " ");
        }
    }
    private static int removeElement(int[] nums, int val) {
        if (nums == null || nums.length == 0) {
            return 0;
        }
        int left = 0;
        int right = nums.length - 1;
        while (left <= right) {
            if (nums[left] != val) {
                left++;
            } else {
                int tmp = nums[left];
                nums[left] = nums[right];
                nums[right] = tmp;
                right--;
            }
        }

        return left;
    }
    private static int removeElement2(int[] nums, int val) {
        if (nums == null) {
            return 0;
        }
        int index = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != val) {
                nums[index++] = nums[i];
            }
        }


        return index;
    }
}
